﻿package mortal.game.view.shop
{
    import flash.events.*;
    import mortal.component.window.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.proxy.*;
    import mortal.game.view.common.*;
    import mortal.game.view.npc.data.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class ShopController extends Controller
    {
        private var _shopModule:ShopModule;
        private var _shopProxy:ShopProxy;

        public function ShopController()
        {
            this.init();
            return;
        }// end function

        private function init() : void
        {
            this._shopProxy = GameProxy.shop;
            return;
        }// end function

        override protected function initView() : IView
        {
            if (this._shopModule == null)
            {
                this._shopModule = new ShopModule();
                this._shopModule.addEventListener(WindowEvent.SHOW, this.onViewShow);
                this._shopModule.addEventListener(WindowEvent.CLOSE, this.onViewClose);
            }
            return this._shopModule;
        }// end function

        override protected function initServer() : void
        {
            Dispatcher.addEventListener(EventName.NPC_OpenNpcShop, this.openShop);
            return;
        }// end function

        private function onViewShow(event:Event) : void
        {
            this.updateBuyBackList();
            CursorManager.currentCurSorType = CursorManager.BUY;
            this._shopModule.addEventListener(MouseEvent.MOUSE_OVER, this.mouse_over);
            this._shopModule.addEventListener(MouseEvent.ROLL_OUT, this.mouse_out);
            NetDispatcher.addCmdListener(ServerCommand.updateBuyBackList, this.updateBuyBackList);
            Dispatcher.addEventListener(EventName.ShopClose, this.closeWin);
            Dispatcher.addEventListener(EventName.ShopBuyBack, this.buyBack);
            return;
        }// end function

        private function onViewClose(event:Event) : void
        {
            this.removeSmallWin();
            this._shopModule.removeEventListener(MouseEvent.ROLL_OVER, this.mouse_over);
            this._shopModule.removeEventListener(MouseEvent.ROLL_OUT, this.mouse_out);
            NetDispatcher.removeCmdListener(ServerCommand.updateBuyBackList, this.updateBuyBackList);
            Dispatcher.removeEventListener(EventName.ShopClose, this.closeWin);
            Dispatcher.removeEventListener(EventName.ShopBuyBack, this.buyBack);
            return;
        }// end function

        private function mouse_over(event:MouseEvent) : void
        {
            switch(CursorManager.currentCurSorType)
            {
                case CursorManager.SELL:
                {
                    CursorManager.showCursor(CursorManager.Arrow);
                    break;
                }
                case CursorManager.FIX:
                {
                    CursorManager.showCursor(CursorManager.FIX);
                    break;
                }
                case CursorManager.BUY:
                {
                    CursorManager.showCursor(CursorManager.BUY);
                    break;
                }
                case CursorManager.Arrow:
                {
                    CursorManager.showCursor(CursorManager.Arrow);
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        private function mouse_out(event:MouseEvent) : void
        {
            CursorManager.showCursor(CursorManager.Arrow);
            return;
        }// end function

        private function openShop(event:DataEvent) : void
        {
            var _loc_2:* = undefined;
            if (event.data is NpcFunctionData)
            {
                _loc_2 = event.data as NpcFunctionData;
            }
            else if (event.data is int)
            {
                _loc_2 = event.data;
            }
            GameManager.instance.popupModuleType(ModuleType.Shops);
            LayerManager.windowLayer.tweenCenterWindow(this.view, GameController.pack.view);
            this._shopModule.updateTabBar(_loc_2);
            return;
        }// end function

        private function removeSmallWin() : void
        {
            if (BuyItemWin.instance && !BuyItemWin.instance.isHide)
            {
                BuyItemWin.instance.hide();
            }
            return;
        }// end function

        private function updateBuyBackList(param1:Object = null) : void
        {
            this._shopModule.updateBuyBackList();
            return;
        }// end function

        private function buyBack(event:DataEvent) : void
        {
            var _loc_2:* = new Array();
            _loc_2.push(event.data);
            this._shopProxy.buyBack(_loc_2);
            return;
        }// end function

        private function closeWin(event:DataEvent) : void
        {
            if (GameController.shop.isViewShow)
            {
                this._shopModule.hide();
            }
            return;
        }// end function

    }
}
